/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.parser;

import org.antlr.runtime.tree.Tree;

import com.nebrija.riif.model.Component;
import com.nebrija.riif.model.Environment;
import com.nebrija.riif.model.Parameter;
import com.nebrija.riif.model.RIIFElement;
import com.nebrija.riif.model.RIIFPropertiesContainer;

/**
 * Process a parameter tag from the tree and creates the RIIF Parameter class 
 * @author asanchep
 *
 */
public class ParameterElementProcessor implements RIIFParserProcessor {

	@Override
	public RIIFElement apply(RIIFElement container, Tree t) {
		if(! (container instanceof RIIFPropertiesContainer)){
			return null;
		}
		String id = t.getChild(0).getText();
		String type = t.getChild(1).getText();
		Parameter par = new Parameter(id);
		par.setProperty(Parameter.TYPE_KEY, type);
		if (t.getChildCount()>2){
			String value = t.getChild(2).getText();
			par.setValue(TypeBuilder.buildType(type, value));
		}
		((RIIFPropertiesContainer)container).addParameter(par);
		
		return par;
	}

}
